﻿using MySqlX.XDevAPI;
using SanlieHash;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Http;
using net_path.data.Database;

namespace net_path.CSM.Common
{
    internal class NetPathSession : IDisposable
    {
        internal NetPathDB db_netpath { get; set; }
        internal IHttpSessionClient client { get; }
        internal HttpContextEventArgs e { get; }
        internal HttpRequest request { get; set; }
        internal HttpResponse response { get; set; }
        public NetPathSession(IHttpSessionClient client, HttpContextEventArgs e)
        {
            db_netpath = new NetPathDB();
            this.client = client;
            this.e = e;
            this.request = e.Context.Request;
            this.response = e.Context.Response;
            IP = client.IP;
        }
        public string IP { get; protected set; }
        public Guid AppID { get; protected set; }
        public Guid OnlineID { get; protected set; }
        public Guid UserID { get; protected set; }
        public bool IsAuthenticated { get; protected set; }
        public string DeviceName { get; protected set; }
        public string DeviceType { get; protected set; }
        public string? AppKey { get; protected set; }
        public bool IsAppAuthenticated { get; protected set; }

        public async Task<T> GetData<T>() where T : class
        {
            return await request.GetDataAsync<T>();
        }

        public bool IsPost(string uri)
        {
            return request.IsPost() && request.UrlEquals(uri);
        }
        public bool IsGet(string uri)
        {
            return request.IsGet() && request.UrlEquals(uri);
        }

        public Task InvokeNext()
        {
            return e.InvokeNext();
        }

        public HttpResponse Send(int status, object obj, string message = "success")
        {
            return response
                        .SetStatus(status, message)
                        .FromObj(obj);
        }

        public void Dispose()
        {
            db_netpath.Dispose();
        }
    }
}
